<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Api\Repositories\WxFeedbackRepositories;

use App\Jobs\UserInputSafeCheckJob;
use App\Models\WxFeedback;
use App\Wen\Utils\UserUtils;
use App\Wen\Utils\Utils;
use Illuminate\Http\Request;

class FeedbackController extends BaseController
{

    /**
     * 用户反馈
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addFeedback(Request $request)
    {

        $uid = $request->uid;

        _limit_user('add:feedback', $uid, 10);

        $feedback_type = $request->feedback_type;
        $feedback_content = strip_tags($request->feedback_content);
        if(strpos(strtolower($feedback_content), '<script') !== false){
            return $this->fail(200004);
        }

        $image_urls = $request->image_urls;

        $image_urls__ = [];
        if($image_urls){
            foreach ($image_urls as $img_item){
                $image_urls__[] = $img_item['url'];
            }
        }

        $contact = $request->contact;

        if(_empty_($feedback_content)){
            $this->fail(200001);
        }

        $r = WxFeedbackRepositories::add($uid, $feedback_type, $feedback_content, $image_urls__, $contact);

        if($r){
            UserInputSafeCheckJob::dispatch($uid, $feedback_content);
            if($image_urls__){
                Utils::image_state_change($image_urls__, 1);
            }
            UserUtils::add_user_experience($uid, 9);
            return $this->success($r);
        }else{
            return $this->fail(200002);
        }
    }

    /*
     * 获取用户的反馈列表
     */
    public function feedbacked(Request $request){
        $uid = $request->uid;
        $feedBacked = WxFeedback::where('user_id', $uid)->orderBy('id', 'desc')->get();

        $res = [];
        if($feedBacked){
            $feedBacked->map(function ($v) use (&$res) {
                $imgs = $v->feedback_imgs ? explode(',', $v->feedback_imgs) : [];
                if($imgs){
                    for ($i = 0; $i < count($imgs); $i++){
                        $imgs[$i] = ['url' => $imgs[$i]];
                    }
                }
                $res[] = [
                    'id' => $v->id,
                    'content' => $v->feedback_content,
                    'reply' => $v->feedback_reply,
                    'imgs' => $imgs,
                    'replay_default' => '您的反馈我们已收到，客服人员会尽快处理并给您答复，到时会有站内im通知，请注意查看',
                    'is_solve' => $v->is_solve,
                    'created_at' => $v->created_at,
                    'updated_at' => $v->updated_at,
                    'state' => _empty_($v->feedback_reply) ? 0 : 1,
                    'stateName' => _empty_($v->feedback_reply) ? '待处理' : '已答复'
                ];
            });
        }

        if(_empty_($res)){
            return $this->fail(200003);
        }

        return $this->success($res);

    }

    /*
     * 更新解决状态
     */
    public function resolve(Request $request){
        $id = $request->id;
        $resolve = $request->resolve;
        $uid = $request->uid;

        if(_empty_($id) || $resolve === null){
            return $this->fail(200001);
        }

        $WxFeedback = WxFeedback::where([
            ['id', '=', $id], ['user_id', '=', $uid]
        ])->first();
        if( $WxFeedback ){
            if($WxFeedback->is_solve == 0){
                $r = WxFeedback::where([
                    ['id', '=', $id], ['user_id', '=', $uid]
                ])->update(['is_solve' => $resolve ? 1 : 2]);
                if($r){
                    return $this->success();
                }
            }else{
                return $this->fail(200010);
            }

            return $this->fail(200002);
        }else{
            return $this->fail(200003);
        }
    }


}
